﻿$(function(){//延迟加载
	$('#subipres').hide();
	var devresId='';
	var djxId='';
	var userange='';
	var gateway='';
	var advlan='';
	var describ='';
	var ipstart='';
	var ipend='';
	var ipsort='';
	var ipdbelong='';
	$('#addip').click(function(){
		 if(!$('#submitipres').form('validate')||$('#userange').combobox('getValue')=='0'||$('#dajuxiang').combobox('getValue')=='0'
			 ||$('#ipsort').combobox('getValue')=='0'||$('#iptype').combobox('getValue')=='0'||$('#iprestype').combobox('getValue')=='0'){
			 alert('资料不完整');
			 return null;
		 }
		devresId=$('#ipsort').combobox('getValue');
		djxId=$('#dajuxiang').combobox('getValue');
		userange=$('#userange').combobox('getValue');
		gateway=$('#ipgateway').val();
		advlan=$('#advlan').val();
		describ=$('#describ').val();
		ipstart=$('#ipstart').val();
		ipend=$('#ipend').val();
		ipsort=$('#iptype').val();
		iprestype=$('#ipres').val();
		ipdbelong=$('#ipdbelong').combobox('getValue');
		$.ajax({
			url:'addipres',
			type:'POST',
			//dataType:'json',
			data:{
				devresId:devresId,
				djxId:djxId,
				ipstart:ipstart,
				ipend:ipend,
				ipdbelong:ipdbelong
			},
			
			success: function(data) {
				if(data.listipd==null){
					alert(data.msg);
					return null;
				}
				$('#subipres').show();
				showipd(data.listipd);
                //alert(data.msg);
                
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
		});
	});
	
	function showipd(data){
		$('#searchres').height('400px');
		$('#ipdeploy').datagrid({
			width:'auto',
			height:'auto',
			rownumbers:true,
			pagination:true,
			fit:true,
			pageSize:20,
			data:data,
			columns:
				[[
				{"field":"djxname",
				"title":"汇接",
				"width":"100px"
				},
				{
				"field":"ipbegin",
				"title":"地址段起始",
				"width":"100px"
				},
				{
				"field":"ipend",
				"title":"地址段终止",
				"width":"100px"
				},
				{
				"field":"gateway",
				"title":"网关",
				"width":"100px"
				},
				{
				"field":"dvbelong",
				"title":"ip类型",
				"width":"70px"
				},
				{
				"field":"status",
				"title":"分配状态",
				"width":"7px"
				},
				{
				"field":"ipdbelong",
				"title":"设备类型",
				"width":"50px"
				},
				{
				"field":"dvname",
				"title":"设备名称",
				"width":"100px"
				}
				]]
		});
		var pager = $("#ipdeploy").datagrid("getPager");  
        pager.pagination({  
            total:data.length,  
            onSelectPage:function (pageNo, pageSize) {  
                var start = (pageNo - 1) * pageSize;  
                var end = start + pageSize;  
                $("#ipdeploy").datagrid('loadData', data.slice(start, end));  
                pager.pagination('refresh', {  
                    total:data.length,  
                    pageNumber:pageNo  
                });  
            }  
        }); 
	}
    $('#subipres').click(function(){
		 if(!$('#submitipres').form('validate')||$('#userange').combobox('getValue')=='0'||$('#dajuxiang').combobox('getValue')=='0'||$('#ipsort').combobox('getValue')=='0'){
			 alert('资料不完整');
			 return null;
		 }
    	var newdata=$('#ipdeploy').datagrid('getData');
    	//alert(devresId);
		$.ajax({
			url:'addipdeploy',
			type:'POST',
			//dataType:'json',
			data:{				
				devresId:devresId,
				djxId:djxId,
				userange:userange,
				gateway:gateway,
				advlan:advlan,
				describ:describ,
				ipstart:ipstart,
				ipend:ipend,	
				ipsort:ipsort,
				ipdbelong:ipdbelong,
				iprestype:iprestype,
				newdata:JSON.stringify(newdata)
			},
			
			success: function(data) {
				//$('#subipres').show();
				//showipd(data.listipd);
				//alert(ipend);
				alert('操作成功！');
				if(opener!=null)
				opener.location.reload();
				else window.location.reload({
					bForceGet:true
				});              
                
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('数据异常 请检查网络');
            	//alert(XMLHttpRequest.status);
                //alert(XMLHttpRequest.readyState);
                //alert(textStatus);
            }
		});
    });
    
    $.extend($.fn.validatebox.defaults.rules, {    
    	 ip : {// 验证IP地址  
             validator : function(value) {  
                 var reg = /^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/ ;  
                 return reg.test(value);  
             	},  
             message : 'IP地址格式不正确'  
    	 	}
    });
});